<@override name="content">

<#if type == 5>
  <#assign typename = "作业" />
</#if>
<#if type == 6>
  <#assign typename = "实验" />
</#if>
<#if type == 7>
  <#assign typename = "实验考试" />
</#if>
<#if type == 8>
  <#assign typename = "课程考试" />
</#if>

<div class="header">
  <h1 class="page-title">C程序设计后台管理</h1>
</div>

<ul class="breadcrumb">
  <li class="active">
    <#if type == 0>
      首页
    <#else>
      ${typename!}管理
    </#if>
  </li>
</ul>

<div class="container-fluid">
  <div class="row-fluid">
    <div class="well">
      <#if type == 0>
        <div id="container">
          <div class="pagination pagination-centered">
            <a href= "cprogram/admin/?type=5" class = "cprog_btn btn btn-large btn-success">作业</a>
            <a href= "cprogram/admin/?type=6" class = "cprog_btn btn btn-large btn-info">实验</a>
            <a href= "cprogram/admin/?type=7" class = "cprog_btn btn btn-large btn-warning">实验考试</a>
            <a href= "cprogram/admin/?type=8" class = "cprog_btn btn btn-large btn-danger">课程考试</a>
          </div>
        </div>
      <#else>
        <#if action?? && action != "add">
          <div class="form-inline">
            <form class="form-search" action="cprogram/admin/search?type=${type}" method="post">
              <select class="input-medium" id="contestID" name="cid">
                <option value="">${typename}名称</option>
                <#list contestList as item>
                  <option value="${item.cid!}" <#if contest?? && contest.cid == item.cid>selected</#if> >
                  ${item.title}
                  </option>
                </#list>
                <option value="-1" <#if allList??> selected </#if> >
                  <#if type == 5>
                    全部作业
                  </#if>
                  <#if type == 6>
                    全部实验
                  </#if>
                  <#if type == 7>
                    任课教师学生实验成绩
                  </#if>
                  <#if type == 8>
                    任课教师学生成绩
                  </#if>
                </option>
              </select>
              <#if type == 6>
                <select class="input-medium" name = "tid" id="teacher_select">
                    <#list teacherList as teacher>
                        <option value="${teacher.uid!}" <#if tid?? && teacher.uid == tid>selected</#if> >
                          ${teacher.realName!}
                        </option>
                    </#list>
                    <option value="-1" <#if tid?? && -1 == tid>selected</#if>>
                        全部教师，如果指定实验名称，将查询实验课时间
                    </option>
                </select>
              </#if>
                <select class="input-medium" id="week_select" name="week">
                  <#list weeks.keySet() as week>
                    <option value="${week!}" <#if WEEK?? && WEEK == week>selected</#if> >
                    ${weeks.get(week!)}
                    </option>
                  </#list>
                </select>
                <select class="input-medium" id="lecture_select" name="lecture">
                  <#list lectures.keySet() as lecture>
                    <option value="${lecture!}" <#if LECTURE?? && LECTURE == lecture>selected</#if> >
                    ${lectures.get(lecture!)}
                    </option>
                  </#list>
                </select>

              <button type="submit" class="btn btn-info" id="sendBtn">提交</button>
              <a class="btn btn-success" href="cprogram/admin?type=${type}&action=add">新建${typename}</a>
            </form>
          </div>
        </#if>
        <#if action?? && action == "add">
          <form class="form-horizontal" id="createContest" action="cprogram/admin/save?type=${type!}" method="post">
            <div class="control-group contest">
              <label class="control-label" for="inputTitle">
                ${typename}内容
              </label>
              <div class="controls">
                <textarea id="inputTitle" name="contest.title" rows="1" cols="40" required>${title!}</textarea>
              </div>
            </div>
            <#if type == 5>
              <div class="control-group">
                <label class="control-label" for="uid">任课教师</label>
                <div class="controls">
                  <div class="input-prepend">
                    <select name = "contest.uid" id="uid">
                        <#list teacherList as teacher>
                          <option value="${teacher.uid!}" <#if teacher.uid == user.uid>selected</#if> >
                            ${teacher.realName!}
                          </option>
                        </#list>
                    </select>
                  </div>
                </div>
              </div>
            </#if>
            <#if type != 6>
              <div class="control-group contest">
                <label class="control-label" for="class_week">
                  <#if type == 5>
                    上课时间
                  <#else>
                      考试时间
                  </#if>
                </label>
                <div class="controls">
                  <div class="input-prepend">
                    <select name = "contest.lockBoardTime" id = "class_week" style="width: 124px">
                      <#if weeks??>
                        <#list weeks.keySet() as week>
                          <option value="${week!}" >${weeks.get(week!)}</option>
                        </#list>
                      </#if>
                    </select>
                    <select name = "contest.unlockBoardTime" id = "class_lecture" style="width: 124px">
                      <#if lectures??>
                        <#list lectures.keySet() as lecture>
                          <option value="${lecture!}">${lectures.get(lecture!)}</option>
                        </#list>
                      </#if>
                    </select>
                  </div>
                </div>
              </div>
            </#if>
            <div class="control-group contest">
              <label class="control-label" for="inputStartTime">开始时间</label>
              <div class="controls input-append date form_datetime" id="startTime">
                <input size="16" type="text" name="startTime" id="inputStartTime" value="${startTime!}" readonly required>
                <span class="add-on"><i class="icon-th" aria-hidden="true"></i></span>
              </div>
            </div>
            <div class="control-group contest">
              <label class="control-label" for="inputEndTime">结束时间</label>
              <div class="controls input-append date form_datetime" id="endTime">
                <input size="16" type="text" name="endTime" id="inputEndTime" value="${endTime!}" readonly required>
                <span class="add-on"><i class="icon-th" aria-hidden="true"></i></span>
              </div>
            </div>
            <div class="control-group">
              <div class="controls">
                <button type="submit" id="submitContest" class="btn btn-primary">提交</button>
                <button type="reset" class="btn btn-info">重置</button>
              </div>
            </div>
          </form>
        </#if>
        <#if action?? && (action == "manager" || action == "edit" || action == "score") >
          <ul class="nav nav-tabs" id="contest_nav" style="width:98%">
            <#if cid != -1>
              <#if serverTime < contest.startTime || adminUser?? || type == 5>
                <li class="<#if action=="manager">active</#if>">
                  <a href="cprogram/admin/?type=${type}&action=manager${query!}">管理</a>
                </li>
                <li class="<#if action=="edit">active</#if>">
                  <a href="cprogram/admin/?type=${type}&action=edit${query!}">编辑</a>
                </li>
              </#if>
            </#if>
            <li class="<#if action=="score">active</#if>">
              <a href="cprogram/admin/?type=${type}&action=score${query!}">成绩</a>
            </li>
          </ul>
        </#if>
        <#if action?? && action == "edit">
          <form class="form-horizontal" id="createContest" action="cprogram/admin/update/?type=${type}${query!}" method="post">
            <div class="control-group contest">
              <label class="control-label" for="inputTitle">
                ${typename}内容
              </label>
              <div class="controls">
                <textarea id="inputTitle" name="contest.title" rows="1" cols="40" required>${contest.title!}</textarea>
              </div>
            </div>
            <#if contest.type == 5>
              <div class="control-group">
                <label class="control-label" for="uid">任课教师</label>
                <div class="controls">
                  <div class="input-prepend">
                    <select name = "contest.uid" id="uid">
                        <#list techerList as teacher>
                          <option value="${teacher.uid!}" <#if teacher.uid == contest.uid>selected</#if> >
                            ${teacher.realName!}
                          </option>
                        </#list>
                    </select>
                  </div>
                </div>
              </div>
            </#if>
            <#if contest.type != 6>
              <div class="control-group contest">
                <label class="control-label" for="class_week">
                  <#if contest.type == 5>
                    上课时间
                  <#else>
                    考试时间
                  </#if>
                </label>
                <div class="controls">
                  <div class="input-prepend">
                    <select name = "contest.lockBoardTime" id ="class_week" style="width: 124px">
                        <#list weeks.keySet() as week>
                          <option value="${week!}" <#if contest?? && week == contest.lockBoardTime >selected</#if> >
                            ${weeks.get(week!)}
                          </option>
                        </#list>
                    </select>
                    <select name = "contest.unlockBoardTime" id = "class_lecture" style="width: 124px">
                        <#list lectures.keySet() as lecture>
                          <option value="${lecture!}" <#if contest?? && lecture==contest.unlockBoardTime >selected</#if> >
                            ${lectures.get(lecture!)}
                          </option>
                        </#list>
                    </select>
                  </div>
                </div>
              </div>
            </#if>
            <div class="control-group contest">
              <label class="control-label" for="inputStartTime">开始时间</label>
              <div class="controls input-append date form_datetime" id="startTime">
                <input size="16" type="text" name="startTime" id="inputStartTime" value="${contest.startDateTime!}" readonly required>
                <span class="add-on"><i class="icon-th" aria-hidden="true"></i></span>
              </div>
            </div>
            <div class="control-group contest">
              <label class="control-label" for="inputEndTime">结束时间</label>
              <div class="controls input-append date form_datetime" id="endTime">
                <input size="16" type="text" name="endTime" id="inputEndTime" value="${contest.endDateTime!}" readonly required>
                <span class="add-on"><i class="icon-th" aria-hidden="true"></i></span>
              </div>
            </div>
            <div class="control-group">
              <div class="controls">
                <button type="submit" id="submitContest" class="btn btn-primary">提交</button>
                <button type="reset" class="btn btn-info">重置</button>
              </div>
            </div>
          </form>
        </#if>
        <#if action?? && action == "manager">
          <h3 class="text-center"> ${contest.title!}</h3>
          <#if contest.type != 6 >
            <h3 class="text-center">${weeks.get(contest.lockBoardTime!)}${lectures.get(contest.unlockBoardTime!)}</h3>
          </#if>
          <div class="row text-center">
            <div class="span8 offset2">
              <div class="well">
                <span>开始时间</span>: <span class="time">${contest.startDateTime!}</span>
                <span>结束时间</span>: <span class="time">${contest.endDateTime!}</span><br>
                <span>当前时间</span>: <span class="time" id="current">${contest.startDateTime!}</span>
              </div>
            </div>
          </div>
          <#if (contest.type == 7 || contest.type == 8) && serverTime < contest.endTime>
            <div class="text-center">
              <form action="cprogram/generate/${contest.cid}" method="post" target="_blank">
                <button type="submit" class="btn btn-success">生成</button>
                <input type='text' name='number' value="${number!}" style="width: 20px" onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();" />
                个密码
              </form>
            </div>
          </#if>
          <div class="row">
            <div class="span8 offset2">

              <table id="problem-list" class="table table-hover table-condensed">
                <thead>
                <tr>
                  <th width="5%">ID</th>
                  <th width="10%">题目编号</th>
                  <th width="30%">标题</th>
                  <th width="10%">时间限制</th>
                  <th width="10%">内存限制</th>
                  <th width="10%">通过数</th>
                  <th width="10%">提交数</th>
                  <#if serverTime  < contest.startTime>
                    <th width="15%">操作</th>
                  </#if>
                </tr>
                </thead>
                <tbody>
                <#if problems??>
                  <#list problems as Problem>
                    <tr class="problem" pid="${Problem.pid!}">
                      <td>
                        <#if serverTime < contest.startTime><i class="icon-resize-vertical"></i></#if>
                <span class="id"></span>
                </td>
                <td class="pid">
                  <a href="problem/show/${Problem.pid!}">${Problem.pid!}</a>
                </td>
                <td class="title">
                  <a href="cprogram/problem/${contest.cid!}-${Problem.id}">${Problem.title!}</a>
                </td>
                <td class="time">${Problem.timeLimit!} MS</td>
                <td class="memory">${Problem.memoryLimit!} KB</td>
                <td class="accept">
                  <a href="cprogram/status/${contest.cid!}?num=${Problem.id!}&result=0">${Problem.accepted!}</a></td>
                <td class="submit">
                  <a href="cprogram/status/${contest.cid!}?num=${Problem.id!}">${Problem.submission!}</a>
                </td>
                <#if serverTime  < contest.startTime>
                  <td class="admin">
                    <button class="btn btn-danger remove" pid="${Problem.pid!}">Remove</button>
                  </td>
                </#if>

                </tr>
                </#list>
                </#if>
                </tbody>
                <#if serverTime < contest.endTime>
                <tfoot>
                <tr>
                  <form class="form-inline" id="addProblem" action="api/contest/addProblem" method="post">
                    <td><input type="hidden" name="cid" value="${contest.cid!}"><span class="id"></span></td>
                    <td><input type="number" name="pid" class="input-small" min="1000" placeholder="Problem ID" required></td>
                    <td><input type="text" name="title" class="input-xlarge" placeholder="Title" required></td>
                    <td></td>
                    <td></td>
                    <td>
                      <button type="submit" class="btn btn-success">添加</button>
                      <#if serverTime  <contest.startTime>
                      <a class="btn btn-info hidden" id="reorder">保存</a>
                      </#if>
                    </td>
                  </form>
                </tr>
                </tfoot>
                </#if>
              </table>
            </div>
          </div>
        </#if>
        <#if action?? && action == "score">
          <#if scoreList??>
            <h3 class="text-center"> ${contest.title!}</h3>
            <#if contest.type != 6 >
              <h3 class="text-center"> ${weeks.get(contest.lockBoardTime!)}${lectures.get(contest.unlockBoardTime!)}</h3>
            </#if>
            <div class="row">
              <div class="span10 offset1">
                <table id="problem-list" class="table table-hover table-condensed">
                  <thead>
                  <tr>
                    <th width="10%">用户名</th>
                    <th width="10%">姓名</th>
                    <th width="10%">学号</th>
                    <th width="10%">班级</th>
                    <th width="10%">通过数</th>
                    <th width="10%">提交数</th>
                    <th width="10%">系统成绩</th>
                    <th width="10%">最终成绩</th>
                  </tr>
                  </thead>
                  <tbody>
                  <#if scoreList??>
                    <#list scoreList as users>
                      <tr>
                        <td>
                          ${users.name!}
                        </td>
                        <td>
                          ${users.realName!}
                        </td>
                        <td>
                          ${users.stuid!}
                        </td>
                        <td>
                          ${users.Class!}
                        </td>
                        <td>
                          <a href="cprogram/status/${contest.cid}?name=${users.stuid!}&result=0" target="_blank">
                            ${users.accepted!}
                          </a>
                        </td>
                        <td>
                          <a href="cprogram/status/${contest.cid}?name=${users.stuid!}" target="_blank">
                            ${users.submited!}
                          </a>
                        </td>
                        <td>
                          ${users.score1!}
                        </td>
                        <td>
                            <a href="#" id="user${users.uid}">
                              ${users.score2!}
                            </a>
                        </td>

                      </tr>
                    </#list>
                  </#if>
                  </tbody>
                </table>
              </div>
            </div>
          </#if>
          <#if allList??>
            <h3 class="text-center"> 总成绩表</h3>
            <h3 class="text-center"> ${weeks.get(WEEK!)!}${lectures.get(LECTURE!)!}</h3>

            <div class="row">
              <div class="span10 offset1">
                <a href="cprogram/admin/getxls?type=${type}${query}" target="_blank" class="bth btn-success btn-small">导出</a>
                <table id="score-list" class="table table-hover table-condensed">
                  <thead>
                    <tr>
                      <th width="10%">用户名</th>
                      <th width="10%">姓名</th>
                      <th width="10%">学号</th>
                      <th width="10%">班级</th>
                      <#assign idx = 1 />
                      <#list workList as contest>
                        <th width="8%">
                          <#if type == 7 || type == 6>实验</#if>
                          <#if type == 8 || type == 5>作业</#if>
                          ${idx}
                          <#assign idx = idx + 1 />
                        </th>
                      </#list>
                      <#if 6 < type >
                        <th width="10%">考试成绩</th>
                        <th width="10%">总成绩</th>
                      </#if>
                    </tr>
                  </thead>
                  <tbody>
                    <#list allList as user>
                      <tr>
                        <td>
                          ${user.name!}
                        </td>
                        <td>
                          ${user.realName!}
                        </td>
                        <td>
                          ${user.stuid!}
                        </td>
                        <td>
                          ${user.Class!}
                        </td>
                        <#list workList as work>
                          <td>
                            <a href="/cprogram/status/${work}?name=${user.stuid!}" target="_blank">
                              <#if user.scoreMap?? && user.scoreMap.get(work)??>
                                  ${user.scoreMap.get(work)}
                              <#else>
                                0
                              </#if>
                            </a>
                          </td>
                        </#list>
                        <#if 6 < type >
                          <td>
                            ${user.examScore!}
                          </td>
                          <td>
                            ${user.finalScore!}
                          </td>
                        </#if>
                      </tr>
                    </#list>
                  </tbody>
                </table>
              </div>
            </div>
          </#if>
        </#if>
      </#if>
    </div>
  </div>
</div>
</@override>
<@override name="styles">
  <link href="assets/css/cprogram.css" type="text/css" rel="stylesheet">
  <#if action??>
    <link href="assets/jquery.artDialog/skins/twitter.css" rel="stylesheet" type="text/css">
  </#if>

</@override>

<@override name="scripts">
  <script src="assets/js/cprogram.js" type="text/javascript"></script>
  <#if type != 0>
    <script type="text/javascript">
        var weekName = new Array();
        var lectureName = new Array();
        <#list contestList as item>
          weekName[${item.cid}] = "${item.week}";
          lectureName[${item.cid}] = "${item.lecture}";
        </#list>

        if($("#contestID").val()==""){
            $("#sendBtn").attr("disabled",true);
        }
        if($("#teacher_select").val() != -1) {
            $("#week_select").attr("disabled", true);
            $("#lecture_select").attr("disabled", true);
            $("#week_select").val("");
            $("#lecture_select").val("");
        }
        $("#teacher_select").click( function () {
            if($("#teacher_select").val() != -1) {
                $("#week_select").attr("disabled", true);
                $("#lecture_select").attr("disabled", true);
                $("#week_select").val("");
                $("#lecture_select").val("");

            }
            else {
                $("#week_select").attr("disabled", false);
                $("#lecture_select").attr("disabled", false);
                <#if WEEK??>
                  $("#lecture_select").val("${LECTURE!}");
                  $("#week_select").val("${WEEK!}");
                <#else>
                $("#lecture_select").val(0);
                $("#week_select").val(0);
                </#if>
            }
        })
        <#if type != 6>
          if($("#contestID").val()!="-1") {
              $("#week_select").attr("disabled", true);
              $("#lecture_select").attr("disabled", true);
              $("#week_select").val(weekName[$("#contestID").val()]);
              $("#lecture_select").val(lectureName[$("#contestID").val()]);
          }
          else {
              <#if WEEK??>
                  $("#lecture_select").val("${LECTURE!}");
                  $("#week_select").val("${WEEK!}");
              <#else>
                  $("#lecture_select").val("0");
                  $("#week_select").val("0");
              </#if>
          }

        </#if>
        $("#contestID").click(function () {
            if($("#contestID").val()==""){
                $("#sendBtn").attr("disabled",true);
            }else{
                $("#sendBtn").attr("disabled",false);
            }

            <#if type != 6 >
                if($("#contestID").val()=="-1"){
                    $("#week_select").attr("disabled",false);
                    $("#lecture_select").attr("disabled",false);
                    <#if WEEK??>
                      $("#lecture_select").val("${LECTURE!}");
                      $("#week_select").val("${WEEK!}");
                    <#else>
                      $("#lecture_select").val("0");
                      $("#week_select").val("0");
                    </#if>
                }
                else {
                    $("#week_select").attr("disabled",true);
                    $("#lecture_select").attr("disabled",true);
                    $("#week_select").val(weekName[$("#contestID").val()]);
                    $("#lecture_select").val(lectureName[$("#contestID").val()]);
                }
            </#if>
        })

    </script>


  </#if>

  <#if action??>
    <#if action == "manager" || action == "score">
      <#if oj_style != "slate">
        <link href="assets/tablecloth/css/tablecloth.css" rel="stylesheet" type="text/css">
        <script src="assets/tablecloth/js/jquery.metadata.js"></script>
        <script src="assets/tablecloth/js/jquery.tablecloth.js"></script>
      </#if>
    </#if>
    <link href="assets/bootstrap-datetimepicker/css/datetimepicker.css" rel="stylesheet">
    <script type="text/javascript" src="assets/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"
            charset="UTF-8"></script>
    <script src="assets/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js"></script>
    <script src="assets/jquery.artDialog/jquery.artDialog.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#inputTitle').tooltip({'trigger': 'focus', 'title': '作业/考试主要内容，如循环结构程序设计。', 'placement': 'right'});
        });
        $(document).ready(function () {
            $('#inputTime').tooltip({'trigger': 'focus', 'title': '上课/考试时间，如周一第二讲。', 'placement': 'right'});
        });

        $('#startTime').datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left",
            //minuteStep: 30

        }).on('changeDate', function (ev) {
            var time = ev.date.valueOf();
            var date = formatDate(new Date(time + 3600000));
            <#if type?? && type <= 6>
            var endDate = formatDate(new Date(time + 7*24*3600*1000));
            <#else>
            var endDate = formatDate(new Date(time + 2*3600*1000));
            </#if>
            $('#inputEndTime').val(endDate);
            $('#endTime').datetimepicker('setStartDate', date);
        });

        $("#endTime").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left",
            startDate: "${startTime!}",
            //minuteStep: 30
        });
    </script>
    <#if action == "manager">
      <script type="text/javascript">
          $(document).ready(function () {
              jQuery.ajaxSetup({
                  cache: true
              });

              clock(function (current_time) {
                  $("#current").html(new Date(current_time).format("yyyy-MM-dd hh:mm:ss"));
              }, 1000);

              function getProblemTitle() {
                  var pid = this.value;
                  if (pid >= 1000) {
                      $.get("/api/problem/getField", {name: "title", pid: this.value},
                          function (data) {
                              if (data.result) {
                                  $("input[name='title']").val(data.result);
                              }
                              else {
                                  $("input[name='title']").val('');
                              }
                          });
                  }
                  else {
                      $("input[name='title']").val('');
                  }
              }

              $("input[name='pid']").change(getProblemTitle).keyup(getProblemTitle);

              // Return a helper with preserved width of cells
              var fixHelper = function (e, ui) {
                  ui.children().each(function () {
                      $(this).width($(this).width());
                  });
                  return ui;
              };

              function updateId() {
                  $("#problem-list tbody tr").each(function (i, e) {
                      var newId = String.fromCharCode(65 + i);
                      $(e).children().children('.id').html(newId);
                  });
              }

              updateId();
              <#if serverTime < contest.startTime>
              $("#problem-list tbody").sortable({
                  helper: fixHelper,
                  connectWith: "tr",
                  stop: function (e, info) {
                      info.item.after(info.item.parents('tr'));
                      $('#reorder').removeClass('hidden');
                      updateId();
                  }
              }).disableSelection();
              </#if>

              <#if oj_style != "slate">
              $("#problem-list").tablecloth({
                  theme: "stats",
                  condensed: true,
                  sortable: false,
                  striped: true,
                  clean: true
              });
              </#if>
              $.ajaxSetup({
                  async : false
              });
              $(function () {
                  $('#addProblem').submit(function () {
                      var that = $(this);
                      var cid = $('input[name=cid]').val();
                      var pid = $('input[name=pid]').val();
                      var title = $('input[name=title]').val();
                      $.post($(this).attr("action"), $('#addProblem').serialize(),
                          function (data) {
                              switch (data.result) {
                                  case -1:
                                      $.dialog({
                                          content: 'Db error.',
                                          time: 1.5
                                      });
                                      break;
                                  case -2:
                                      $.dialog({
                                          content: 'Too many problems in this contest.',
                                          time: 1.5
                                      });
                                      break;
                                  case -3:
                                      $.dialog({
                                          content: 'Duplicate problems for this contest.',
                                          time: 1.5
                                      });
                                      break;
                                  case -4:
                                      $.dialog({
                                          content: 'This problem does not exist.',
                                          time: 1.5
                                      });
                                      break;
                                  case -5:
                                      $.dialog({
                                          content: 'Contest already finished.',
                                          time: 1.5
                                      });
                                      break;
                                  default:
                                      var timeLimit = 0;
                                      var memoryLimit = 0;
                                      $.get("api/problem/getField", {name: "timeLimit", pid: pid},
                                          function (dataTimeLimit) {
                                              if (dataTimeLimit.result) {
                                                  timeLimit = dataTimeLimit.result;

                                              }
                                              else {
                                                  timeLimit = 0;
                                              }
                                          });
                                      $.get("api/problem/getField", {name: "memoryLimit", pid: pid},
                                          function (dataMemoryLimit) {
                                              if (dataMemoryLimit.result) {
                                                  memoryLimit = dataMemoryLimit.result;
                                              }
                                              else {
                                                  memoryLimit = 0;
                                              }
                                          });
                                      var html = '<tr class="problem" pid="' + pid + '">' +
                                          '<td><i class="icon-resize-vertical"></i><span class="id"></span></td>'
                                          + '<td class="pid"><a href="problem/show/' + pid + '" target="_blank">' + pid + '</a></td>'
                                          + '<td class="title"><a href="cprogram/problem/' + cid + '-' + String.fromCharCode(65 + data.result) + '">' + title + '</a></td>'
                                          + '<td class="time">' + timeLimit + 'MS</td>'
                                          + '<td class="memory">' + memoryLimit + 'KB</td>'
                                          + '<td class="accept"><a href="cprogram/status/' + cid + '?num=' + String.fromCharCode(65 + data.result) + '&result=0">0</a></td>'
                                          + '<td class="submit"><a href="cprogram/status/' + cid + '?num=' + String.fromCharCode(65 + data.result) + '">0</a></td>'

                                          <#if serverTime  < contest.startTime>
                                            + '<td class="admin">' +
                                              '<button class="btn btn-danger remove" pid="' + pid + '">Remove</button>\n' +
                                            '</td>' +
                                          </#if>

                                      '</tr>';
                                      $('#problem-list tbody').append(html);
                                      updateId();
                                      break;
                              }
                          });
                      return false;
                  });
                  $('#reorder').click(function () {
                      var data = {cid:${contest.cid!}, pid: ''};
                      $("#problem-list tbody tr.problem").each(function (i, e) {
                          data.pid += $(e).attr('pid') + ',';
                      });
                      data.pid = data.pid.substring(0, data.pid.length - 1);
                      $.post('api/contest/reorderProblem', data, function (data) {
                          if (data.success) {
                              $.dialog({
                                  content: 'Reorder problem success.',
                                  time: 1
                              });
                          }
                          else {
                              $.dialog({
                                  content: data.result,
                                  time: 1.5
                              });
                          }
                      });
                  });
                  $('table').on('click', '.remove', function () {
                      var that = $(this);
                      $.dialog({
                          title: 'Remove problem from contest',
                          content: 'Are you sure?',
                          cancelVal: 'Cancel',
                          cancel: true,
                          okVal: 'Remove',
                          ok: function () {
                              $.post('api/contest/removeProblem', {cid:${contest.cid!}, pid: that.attr('pid')}, function (data) {
                                  if (data.success) {
                                      that.parent().parent().remove();
                                      updateId();
                                      $.dialog({
                                          content: 'Problem removed.',
                                          time: 1
                                      });
                                  }
                                  else {
                                      $.dialog({
                                          content: data.result,
                                          time: 1.5
                                      });
                                  }
                              });
                              this.close();
                              return false;
                          }
                      });
                  });
              });
          });
      </script>
    </#if>
    <#if action == "score" && !(allList??)>
      <link href="assets/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet">
      <script src="assets/bootstrap-editable/js/bootstrap-editable.js"></script>
      <script>
          $(document).ready(function () {
              var apiUrl = '/cprogram/updateFinalScore/${contest.cid!}';
              $.fn.editable.defaults.mode = 'inline';
              $.fn.editable.defaults.url = apiUrl;
              <#if scoreList??>
              <#list scoreList as users >
              $('#user${users.uid!}').editable({
                  type: 'text',
                  pk : 0,
                  params: {type: 'string'},
                  title: 'Change Final Score'
              });
              </#list>
              </#if>
          });
      </script>
    </#if>
  </#if>
</@override>
<@extends name="_layout.html"></@extends>
